// packageE/healthy/component/healthy_select/healthy_select.js
Component({
  /**
     * 组件的属性列表
     */
  properties: {
    question:{
      type:Object,
      value:{}
    },
    answer:{
      type:Object,
      value:{}
    },
    site:{
      type:Number,
      value:0
    }
  },

  /**
     * 组件的初始数据
     */
  data: {
    selectedArr:[]
  },
  ready(){
    console.log(this.data.answer.option);
    let option = this.data.answer.option;
    console.log(option);
    let {question} = this.data;
    for(let i=0;i<question.option.length;i++){
      if(option.some(sub=>sub.option_id==question.option[i].option_id)){
        question.option[i].selected = true;
      }
    }
    this.setData({
      question,
      selectedArr:option
    });
        
  },
  /**
     * 组件的方法列表
     */
  methods: {
    selectedItem(event){
      console.log(event.currentTarget.dataset.item);
      let item = event.currentTarget.dataset.item;
      let {selectedArr,question} = this.data;
           
      // if(selectedArr.some(sub=>sub.option_id==item.option_id)){
      //     console.log("youmeyou zhengquede");
      //     question.option[i].selected = true;
      // }
      //  selectedArr.push(item)
      // console.log(selectedArr.some(sub=>sub.option_id==item.option_id));
      // return;
      if(question.is_option==1){
        let current = selectedArr.some(sub=>sub.option_id==item.option_id);
        if(selectedArr.length!=0){
          for(let i=0;i<question.option.length;i++){
            if(question.option[i].option_id == selectedArr[0].option_id){
              question.option[i].selected = false;
            }
          }
          selectedArr = [];
        }
        console.log(current);
        if(!current && !selectedArr.some(sub=>sub.option_id==item.option_id)){
          delete item.selected;
          selectedArr.push(item);
          for(let i=0;i<question.option.length;i++){
            if(question.option[i].option_id == item.option_id){
              question.option[i].selected = true;
            }
          }
        }
      }else if(question.is_option==2){
        if(!selectedArr.some(sub=>sub.option_id==item.option_id)){
          selectedArr.push(item);
          for(let i=0;i<question.option.length;i++){
            if(question.option[i].option_id == item.option_id){
              question.option[i].selected = true;
            }
          }
        }else {
          for(let i=0;i<question.option.length;i++){
            if(question.option[i].option_id == item.option_id){
              question.option[i].selected = false;
            }
          }
          selectedArr.forEach((sup,key)=>{
            if(sup.option_id == item.option_id){
              selectedArr.splice(key,1);
            }
          });
        }
      }
            
      this.setData({question,selectedArr});
      this.triggerEvent("quesCallback",{
        question:this.data.question,
        site:this.data.site,
        answer:selectedArr
      });

    }
  }
});
